/*
Author:kermit
Email:455196886@qq.com
Company:http://dragontrail.com/
Creattime:2017-6-26
*/

const isPro = true;//生产环境设置
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');//打开浏览器
const ExtractTextPlugin = require('extract-text-webpack-plugin');//提取样式文件

//处理css样式
const cssLoaderConfig = {
	loader: 'css-loader',
	options: {
		minimize:  isPro ? true : false,//压缩 css
		importLoaders:1//处理样式中import引入进来的文件
	}
}
//浏览器前缀自动补全
const postcssLoaderConfig = {
	loader: 'postcss-loader',//自动添加前缀的插件
	options: {
		plugins: function () {
			return [require('autoprefixer')];
		}
	}
}

//文件路径
const urlLoader = {
	loader:'url-loader',
	options:{	
		limit:8192,
		name:'[path][name].[hash:7].[ext]'
	}
}

//文件加载
const fileLoader = {
	loader:'file-loader',
	options:{
		name:'[path][name].[hash:7].[ext]'
	}
}

//分离样式
const extractCSS = new ExtractTextPlugin({
	filename:'[name].[contenthash]-css.css',
	allChunks:true,
});

module.exports = {
	devtool:'eval-source-map',//配置生成Source Maps，选择合适的选项
	context:path.resolve(__dirname + '/src'),//基础目录，绝对路径,用于从配置中解析入口起点(entry point)和 loader
	entry:{
		app:'./app.js',//app入口
		index:'./js/index.js',//主页入口
		about:'./js/about.js',//关于页面入口
		vendor: ['./js/lib/flexible.js','jquery'],// 第三方依赖名
	},
	output:{
		filename:'[name].bundle.js',//打包后输出文件的文件名
		path:__dirname + '/dist',//打包后的文件存放的地方
		publicPath:''
	},
	module:{
	    rules: [
	      	{
	        	test: /\.json$/,
	        	use: 'json-loader'
	      	},
	      	{
	      		test:/\.js$/,
				exclude: path.resolve(__dirname,'node_modules'),// 排除node_modules目录下的文件
				include: path.resolve(__dirname,'src'), // 只对src目录下的文件进行处理
				use:[{
			        loader: 'babel-loader',
			        options: {
			          presets: ['es2015'],
			        }
			    }]
	      	},
	      	{
	      		test:/\.css$/,
	      		use:extractCSS.extract([
      				cssLoaderConfig,
      				postcssLoaderConfig,
      			])
	      	},
	      	{
	      		test:/\.less$/,
	      		use:extractCSS.extract([
      				cssLoaderConfig,
      				postcssLoaderConfig,
      				'less-loader',
	      		])
	      	},
	      	{
	      		test:/\.scss$/,
	      		use:extractCSS.extract([
      				cssLoaderConfig,
      				postcssLoaderConfig,
      				'sass-loader',
	      		])
	      	},
		    {
		        test: /\.(jpe?g|png|gif|svg)$/i,
		        use:[fileLoader,'image-webpack-loader']
		    },
			{
				test: /\.(woff2?|eot|ttf|otf)$/,
				use:[urlLoader]
			},
			{
				test: /\.html$/,
				use: [{
					loader: 'html-loader',
					options: {
						minimize:  isPro ? true : false //压缩html
					}
				}]
			}
	    ]
	},
    plugins: [
	    extractCSS,
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: function (module) {
               // this assumes your vendor imports exist in the node_modules directory
               return module.context && module.context.indexOf('node_modules') !== -1;
            }
        }),
        //CommonChunksPlugin will now extract all the common modules from vendor and main bundles
        new webpack.optimize.CommonsChunkPlugin({
            name: 'manifest' //But since there are no more common modules between them we end up with just the runtime code included in the manifest file
        }),
	    //压缩混淆js 
	    new webpack.optimize.UglifyJsPlugin(),
		new HtmlWebpackPlugin({
			filename:'./index.html',
			template: './pages/index.html'
		}),
		new HtmlWebpackPlugin({
			filename:'./about.html',
			template: './pages/about.html'
		})
    ]
}